package com.yourvote.dao.impl;

import com.yourvote.dao.CandidateDao;
import com.yourvote.model.Candidate;

import javax.persistence.Query;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: pavlo.fedyna
 * Date: 10/25/12
 * Time: 4:28 PM
 * To change this template use File | Settings | File Templates.
 */
public class CandidateDaoImpl extends BaseDaoImpl implements CandidateDao{
    @Override
    public List<Candidate> getAll() {
        return (List<Candidate>) getAll("select c from Candidate c");
    }

    @Override
    public void add(Candidate candidate) {
        persist(candidate);
    }

    @Override
    public Candidate getCandidate(String name) {
        Query query = getEM().createQuery("select c from  Candidate c where c.name = :name");
        query.setParameter("name",name);
         return (Candidate) query.getSingleResult();
    }

    @Override
    public void remove(Candidate candidate) {
        Candidate candidate1 = getEM().find(Candidate.class,candidate);
        getEM().remove(candidate1);
    }
}
